//
//  CompositionViewController.m
//  work
//
//  Created by 徐宏飞 on 16/5/26.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "CompositionViewController.h"
#import "WorkerCompleteCollectionViewCell.h"
#import "XHF_LbumViewController.h"  //相册
#import "XRCarouselView.h"          //轮播图
#import "HomeListModel.h"           //数据


static NSString *collectionViewCell_ID = @"WorkerCompleteCollectionViewCell";


@interface CompositionViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XRCarouselViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *imgHeader;

@property (weak, nonatomic) IBOutlet UILabel *lblWorkerName;
@property (weak, nonatomic) IBOutlet UILabel *lblAddress;           //地址
@property (weak, nonatomic) IBOutlet UILabel *lblConstructionTime;  //施工时间
@property (weak, nonatomic) IBOutlet UILabel *lblFloorArea;         //面积
@property (weak, nonatomic) IBOutlet UILabel *lblPrice;
@property (weak, nonatomic) IBOutlet UILabel *lblRedpaper;

@property (weak, nonatomic) IBOutlet UICollectionView *renderingCollectionView;
@property (nonatomic ,strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) XRCarouselView *carouselView;  //轮播图
@property (nonatomic, strong) NSMutableArray *imgCarouseArrays;      //设计图小图
@property (nonatomic, strong) NSMutableArray *imgCarouseList;        //设计图大图


@property (nonatomic, strong) NSMutableArray *construction_picArray; //施工图小图
@property (nonatomic, strong) NSMutableArray *construction_picList;  //施工图大图


@end

@implementation CompositionViewController


#pragma mark - life cycle

- (void)initNavigationBar
{
    self.enableReturnButton = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    self.title = @"详情";
    
    
    
    [self configurationHeaderView];
    [self configurationRenderCollectionView];
    
    [self _getConstruction_Info];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    [MobClick beginLogPageView:@"Home-details"];//("PageOne"为页面名称，可自定义)
    
}



- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"Home-details"];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.construction_picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkerCompleteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell_ID forIndexPath:indexPath];
    cell.deleteBtn.hidden = YES;
    NSString *obj = [self.construction_picArray objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:obj];
    [cell.imgComplete sd_setImageWithURL:url placeholderImage:IMAGEPLACEHOLDER];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.construction_picArray.count == 0 || self.construction_picArray.count < indexPath.row) {
        return;
    } else {
        //进入相册
        XHF_LbumViewController *lbumVC = [[XHF_LbumViewController alloc] init];
        lbumVC.imgArr = self.construction_picList;
        lbumVC.currentIndex = indexPath.row;
        lbumVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:lbumVC animated:YES completion:nil];
    }
}









#pragma mark - CustomDelegate
- (void)carouselView:(XRCarouselView *)carouselView didClickImage:(NSInteger)index {
//    NSLog(@"点击了第%ld张图片", index);
    if (self.construction_picArray.count == 0 || self.construction_picArray.count < index) {
        return;
    } else {
        //进入相册
        XHF_LbumViewController *lbumVC = [[XHF_LbumViewController alloc] init];
        lbumVC.imgArr = self.imgCarouseList;
//        NSLog(@"AA%@", self.imgCarouseList);
        lbumVC.currentIndex = index;
        lbumVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:lbumVC animated:YES completion:nil];
    }
    
}





#pragma mark - event response



#pragma mark - private methods
- (void)configurationRenderCollectionView{
    //item 比例4:5
    CGSize size = CGSizeMake(118, 118);
    //4  4s  5  5s
    if (SCREEN_WIDTH == 320) {
        size = CGSizeMake(86, 86);
    }
    
    //6 6s
    if (SCREEN_WIDTH == 375) {
        size = CGSizeMake(105, 105);
    }
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = size;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    self.flowLayout.minimumLineSpacing = 12;
    self.flowLayout.minimumInteritemSpacing = 8;


    _renderingCollectionView.collectionViewLayout = self.flowLayout;
    _renderingCollectionView.backgroundColor = UIColorFromRGB(0xEBEBEB);

    [_renderingCollectionView registerNib:[UINib nibWithNibName:@"WorkerCompleteCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:collectionViewCell_ID];
}

- (void)configurationHeaderView{
    //创建轮播图
    
    
    self.carouselView = [XRCarouselView carouselViewWithImageArray:self.imgCarouseArrays describeArray:nil];
    
    //设置frame
    self.carouselView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
    
    self.carouselView.delegate = self;
    
    
    _carouselView.changeMode = ChangeModeFade;
    
    _carouselView.time = 4;
    
    _carouselView.pagePosition = PositionBottomRight;
    
    [self.imgHeader addSubview:_carouselView];

}

- (void)_getConstruction_Info{
    
    self.lblWorkerName.text = [NSString stringWithFormat:@"%@", _model.type_name];
    
    //地址
    self.lblAddress.text = [NSString stringWithFormat:@"%@", _model.address];
    //施工时间
    self.lblConstructionTime.text = [NSString stringWithFormat:@"施工时间: %@至%@", [NSString getBeijingDayWithStrTime:_model.start_time], [NSString getBeijingDayWithStrTime:_model.end_time]];
    //面积
    self.lblFloorArea.text = [NSString stringWithFormat:@"面积: %@平米", _model.square];
    //金额
    self.lblPrice.text = [NSString stringWithFormat:@"金额: %@元/平米x%@平米=%@元", _model.price, _model.square, _model.amount];
}





#pragma mark - gettsers and setters
- (NSMutableArray *)construction_picArray{
    if (!_construction_picArray) {
        _construction_picList = [NSMutableArray array];
        _construction_picArray = [NSMutableArray array]; //小图
        //分隔成数组
        NSArray *array = [self.model.construction_piclist componentsSeparatedByString:@","];
        for (NSString *str in array) {
            if (![str isEqualToString:@""]) {
                NSString *result = [str stringByAppendingString:avatarcase];
                [_construction_picArray addObject:result];
                [_construction_picList addObject:str];
            }
        }
    }
    
    return _construction_picArray;
}

- (NSArray *)imgCarouseArrays{
    if (!_imgCarouseArrays) {
        _imgCarouseList = [NSMutableArray array];   //大图
        _imgCarouseArrays = [NSMutableArray array]; //小图
        //分隔成数组
        NSArray *array = [self.model.design_piclist componentsSeparatedByString:@","];
        for (NSString *str in array) {
            if (![str isEqualToString:@""]) {
                NSString *result = [str stringByAppendingString:caroselmain];
                [_imgCarouseArrays addObject:result];
                [_imgCarouseList addObject:str];
            }
        }
    }
    return _imgCarouseArrays;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
